package com.yx.modules.api.controller;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.Dto.TbAttentionDto;
import com.yx.modules.api.Dto.TbAuthorDto;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.entity.TbAttention;
import com.yx.modules.api.entity.TbUser;
import com.yx.modules.api.service.TbAttentionService;
import com.yx.modules.api.service.TbUserService;
import com.yx.pc.api.Dto.AttentionDto;
import com.yx.system.entity.TdBook;
import com.yx.system.entity.TdSection;
import com.yx.system.service.TdBookService;
import com.yx.system.service.TdSectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 关注
 *
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-08 17:10:44
 */
@RestController
@RequestMapping("/api/attention")
public class TbAttentionController {
    @Autowired
    private TbAttentionService attentionService;

    @Autowired
    private TdBookService tdBookService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TdSectionService tdSectionService;

    /**
     * 查询用户的关注
     */
    @RequestMapping("/list")
    public R list(@LoginUser TbUser tbUser, @RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        query.put("userId", tbUser.getUuid());
        //初始化数据传输对象
        List<TbAttentionDto> attentionDtos = new ArrayList<>();

        List<TbAttention> attentionList = attentionService.queryList(query);
        int total = attentionService.queryTotal(query);

        for (TbAttention attention : attentionList) {
            TbAttentionDto tbAttentionDto = new TbAttentionDto();
            List<TdBook> tdBooks = tdBookService.findByAuthorCode(attention.getAuthorId());
            tbAttentionDto.setTbAttention(attention);
            tbAttentionDto.setTdBooks(tdBooks);
            attentionDtos.add(tbAttentionDto);
        }

        PageUtils pageUtil = new PageUtils(attentionDtos, total, query.getLimit(), query.getPage());

        return R.ok().put("data", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TbAttention attention = attentionService.queryObject(id);

        return R.ok().put("attention", attention);
    }

    /**
     * 关注作者
     */
    @RequestMapping("/addAttention")
    public R save(@LoginUser TbUser tbUser, String authorId) {
        //通过作家id和身份去查询作家
        TbUser author = tbUserService.findByAuthor(authorId);
        if (author == null) {
            return R.error("没有此作者");
        }
        //通过作家编码和用户编码去查询是否已经关注
        TbAttention tbAttention1 = attentionService.findByUserAndAuthor(tbUser.getUuid(), author.getUuid());
        if (tbAttention1 != null) {
            return R.error("你已经关注此作家了");
            //没关注就创建关注
        } else {
            TbAttention tbAttention = new TbAttention();
            tbAttention.setUserId(tbUser.getUuid());
            tbAttention.setAuthorId(author.getUuid());
            tbAttention.setAuthorName(author.getUsername());
            tbAttention.setAuthorPic(author.getProfileimageurl());
            tbAttention.setAttentionDate(new Date());
            //根据作家查询图书
            List<TdBook> tdBooks = tdBookService.findByAuthorCode(author.getUuid());
            if (tdBooks.size() == 0) {
                tbAttention.setProductionNumber(0);
            } else {
                tbAttention.setProductionNumber(tdBooks.size());
            }
            attentionService.save(tbAttention);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbAttention attention) {
        attentionService.update(attention);

        return R.ok();
    }

    /**
     * 批量删除
     */
    @RequestMapping("/delete")
    public R delete(Integer[] ids) {
        attentionService.deleteBatch(ids);
        return R.ok();
    }

    /*
    * 查看作者首页
    */
    @AuthIgnore
    @RequestMapping("/authorInfo")
    public R authorInfo(String authorId){
        //初始化DTO对象
        TbAuthorDto tbAuthorDto = new TbAuthorDto();
        //根据id查询作者
        TbUser tbUser = tbUserService.findByAuthor(authorId);
        if (tbUser==null){
            return R.error("查无此作者");
        }
        //根据作者去他的图书
        List<TdBook> tdBooks = tdBookService.findByAuthorCode(tbUser.getUuid());
        if (tdBooks.size()!=0){
            tbAuthorDto.setTdBooks(tdBooks);
            //初始化字数
            Integer fontCount = 0;
            //循环图书取得总创作字数
            for (TdBook tdBook:tdBooks){
                fontCount += tdBook.getFontCount();
            }
            //注入创作总字数
            tbAuthorDto.setFontCount(fontCount);
        }
        tbAuthorDto.setTbUser(tbUser);

        return R.ok().put("data",tbAuthorDto);
    }
}
